package com.xc.erp.modules.erp.controller.account;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.common.utils.SecurityUtils;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.dto.JshAccountParam;
import com.xc.erp.modules.erp.domain.pojo.JshAccount;
import com.xc.erp.modules.erp.service.JshAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 账户信息控制器
 *
 * @author wjw 2022/12/21
 * @DateTime: 2022/12/21 14:06
 */
@RestController
@RequestMapping("/account")
@Api(tags = "账户信息Api")
public class JshAccountController {

    @Autowired
    private JshAccountService jshAccountService;

    @ApiOperation("查询所有账户信息")
    @GetMapping("list/{page}/{limit}")
    public AjaxResult list(@PathVariable Integer page, @PathVariable Integer limit, JshAccountParam jshAccountParam){
        IPage<JshAccount> iPage = jshAccountService.listPageAccount(page,limit,jshAccountParam);
        return AjaxResult.success(iPage);
    }

    @Log(modul = "账户信息",desc = "添加账户")
    @ApiOperation("添加账户信息")
    @PostMapping("add")
    public AjaxResult add(JshAccount jshAccount){
        jshAccount.setEnabled(true);
        jshAccount.setIsDefault(false);
        jshAccount.setTenantId(SecurityUtils.getLoginUser().getJshUser().getTenantId());
        boolean result = jshAccountService.save(jshAccount);
        if(result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("添加异常");
        }
    }

    @Log(modul = "账户信息",desc = "修改账户")
    @ApiOperation("修改账户信息")
    @PutMapping("update")
    public AjaxResult update(JshAccount jshAccount){
        boolean result = jshAccountService.updateById(jshAccount);
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改异常");
        }
    }

    @Log(modul = "账户信息",desc = "删除账户")
    @ApiOperation("删除账户信息")
    @DeleteMapping("batchDel")
    public AjaxResult removeById(@RequestBody Long[] ids){
        boolean result = jshAccountService.removeBatchByIds(Arrays.asList(ids));
        if (result){
            return AjaxResult.success();
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    @Log(modul = "账户信息",desc = "批量修改账户")
    @ApiOperation("批量修改修改")
    @PutMapping("batchUpdateStatus/{status}")
    public AjaxResult batchUpdateStatus(@PathVariable boolean status,@RequestBody Long[] ids){
        boolean flag = jshAccountService.batchUpdateStatus(status,ids);
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @ApiOperation("检查账户是否存在")
    @GetMapping("checkAccountByName/{name}")
    public AjaxResult checkAccountByName(@PathVariable String name){
        boolean flag = jshAccountService.checkAccountByName(name);
        return AjaxResult.success().put("status",flag);
    }

    @Log(modul = "账户信息",desc = "删除账户")
    @ApiOperation("根据账户编号删除信息")
    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable long id){
        boolean result = jshAccountService.removeById(id);
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("删除异常");
        }
    }

    @Log(modul = "账户信息",desc = "修改账户默认状态")
    @ApiOperation("修改默认状态")
    @PostMapping("updateIsDefault/{id}")
    public AjaxResult idDefault(@PathVariable Long id,@Param("isDefault") boolean isDefault){
        boolean flag = jshAccountService.updateByIsDefault(id,isDefault);
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改异常");
        }
    }
}
